#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk
from typing import Tuple

from AbstractGrid import AbstractGrid
from a2_solution import Position, Game
from constants import *


class BasicMap(AbstractGrid):

    def __init__(self, master: tk.Tk, size: Tuple[int, int], **kwargs):
        """
        The size parameter is the number of rows (= number of columns) in the grid. Each rectangle should be 50
        pixels high and 50 pixels wide. You should set the background colour of the BasicMap instance
        by using the kwargs.
        Args:
            master:
            size:
            **kwargs:
        """
        rows, cols = size
        py = rows * CELL_SIZE
        px = cols * CELL_SIZE
        AbstractGrid.__init__(self, master, rows, cols, px, py, bg=LIGHT_BROWN, **kwargs)

    def draw_entity(self, position: Position, tile_type: str):
        """
        Draws the entity with tile type at the
        given position using a coloured rectangle with superimposed text identifying the entity.
        Args:
            position:
            tile_type:

        Returns:

        """
        # AbstractGrid.annotate_position(self,position,tile_type)
        bg_color = ENTITY_COLOURS.get(tile_type)

        font_color = BLACK  # black
        if tile_type in (PLAYER, HOSPITAL):
            font_color = WHITE  # white

        x_min, y_min, x_max, y_max = self.get_bbox(position)
        rect = self.create_rectangle(x_min, y_min, x_max, y_max, fill=bg_color)
        x, y = self.get_position_center(position)
        label = self.create_text(x, y,text=tile_type, font=FONT_TEXT,fill=font_color)

    def draw(self,game:Game):
        """
        clear first and then call self.draw_entity for every entity
        Args:
            game:

        Returns:

        """
        self.delete('all')
        for position, entity in game.get_grid().get_mapping().items():
            self.draw_entity(position, entity.display())